<html>
<head><meta charset="utf-8"><title>Conversion of `bumpalo` to use `alloc_wg` crate · t-libs/wg-allocators · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/index.html">t-libs/wg-allocators</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/Conversion.20of.20.60bumpalo.60.20to.20use.20.60alloc_wg.60.20crate.html">Conversion of `bumpalo` to use `alloc_wg` crate</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="179644886"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/Conversion%20of%20%60bumpalo%60%20to%20use%20%60alloc_wg%60%20crate/near/179644886" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Erich Gubler <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/Conversion.20of.20.60bumpalo.60.20to.20use.20.60alloc_wg.60.20crate.html#179644886">(Nov 01 2019 at 17:13)</a>:</h4>
<p>Hey all! As part of learning about how to actually use the APIs we currently have I'm experimenting with changing <code>bumpalo</code> to use the <code>alloc_wg</code> crate. I think I've been able to figure out how to use some things, so I'll have lots of questions to check that my understanding is right soon. :)</p>



<a name="179644992"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/Conversion%20of%20%60bumpalo%60%20to%20use%20%60alloc_wg%60%20crate/near/179644992" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Erich Gubler <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/Conversion.20of.20.60bumpalo.60.20to.20use.20.60alloc_wg.60.20crate.html#179644992">(Nov 01 2019 at 17:15)</a>:</h4>
<p>I've been adding the necessary fallible APIs for <code>bumpalo</code>'s forked definitions of <code>String</code> and <code>Vec</code>, but I'm worried I may be reinventing the wheel. Has that already been done?</p>



<a name="179646255"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/Conversion%20of%20%60bumpalo%60%20to%20use%20%60alloc_wg%60%20crate/near/179646255" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Tim Diekmann <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/Conversion.20of.20.60bumpalo.60.20to.20use.20.60alloc_wg.60.20crate.html#179646255">(Nov 01 2019 at 17:30)</a>:</h4>
<p>Currently I have only implemented <code>Box</code> and <code>RawVec</code>. <code>Vec</code> and <code>String</code> would be the logical next step (in this order). Feel free to make PRs to the <code>alloc-wg</code> crate :)</p>



<a name="179646564"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/Conversion%20of%20%60bumpalo%60%20to%20use%20%60alloc_wg%60%20crate/near/179646564" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Tim Diekmann <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/Conversion.20of.20.60bumpalo.60.20to.20use.20.60alloc_wg.60.20crate.html#179646564">(Nov 01 2019 at 17:34)</a>:</h4>
<p>Regarding the fallible API: I introduced a new Never-type for stable-compatibility (this will be replaced with <code>!</code> asap). As soon as an allocator uses this as the error type, the infallible methods on <code>Box</code> and <code>RawVec</code> can be used. <code>AbortAlloc</code> wraps any <code>BuildAlloc</code> and <code>AllocRef</code> and makes them infallible.</p>



<a name="179650724"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/Conversion%20of%20%60bumpalo%60%20to%20use%20%60alloc_wg%60%20crate/near/179650724" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Erich Gubler <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/Conversion.20of.20.60bumpalo.60.20to.20use.20.60alloc_wg.60.20crate.html#179650724">(Nov 01 2019 at 18:19)</a>:</h4>
<p>What's the reason for a separate <code>Never</code> type when <code>core::convert::Infallible</code> is available? Does it predate that, maybe?</p>



<a name="179665816"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/Conversion%20of%20%60bumpalo%60%20to%20use%20%60alloc_wg%60%20crate/near/179665816" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Tim Diekmann <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/Conversion.20of.20.60bumpalo.60.20to.20use.20.60alloc_wg.60.20crate.html#179665816">(Nov 01 2019 at 21:07)</a>:</h4>
<p>It's actually <a href="https://timdiekmann.github.io/alloc-wg/alloc_wg/type.Never.html" target="_blank" title="https://timdiekmann.github.io/alloc-wg/alloc_wg/type.Never.html">an alias</a> ;)</p>



<a name="179723855"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/Conversion%20of%20%60bumpalo%60%20to%20use%20%60alloc_wg%60%20crate/near/179723855" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Erich Gubler <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/Conversion.20of.20.60bumpalo.60.20to.20use.20.60alloc_wg.60.20crate.html#179723855">(Nov 02 2019 at 23:45)</a>:</h4>
<p>Okay, one hitch I'm not immediately sure how to resolve, so I'll shelf this for a bit in favor of developing the summary I've talked about: the only part of the pre-allocators API that doesn't seem to match now is actually the interface of <code>Clone</code>. Right now, the only way to get a <code>&lt;B as BuildAlloc&gt;::Ref</code> from a <code>RawVec</code> (the <code>Vec::buf</code> field, in this case) is with the <code>RawVec::alloc_ref()</code> method, but...that takes <code>&amp;mut self</code>. Which obviously can't happen in an implementation of <code>Clone</code> without some form of synchronization.</p>



<a name="179728952"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/Conversion%20of%20%60bumpalo%60%20to%20use%20%60alloc_wg%60%20crate/near/179728952" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Tim Diekmann <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/Conversion.20of.20.60bumpalo.60.20to.20use.20.60alloc_wg.60.20crate.html#179728952">(Nov 03 2019 at 02:50)</a>:</h4>
<p><code>alloc_ref</code> takes a <code>&amp;mut self</code> as <code>build_alloc_ref</code> requires <code>&amp;mut self</code>. This was discussed in the issue about <code>BuildAlloc</code> as well (Can't remember if it was in the issue directly or in the old issue on /rust-lang/rust). </p>
<p>I have not implemented <code>Clone</code> on <code>RawVec</code> but I think it's very similar to the <code>Box</code>. I implemented <code>Clone</code> on <code>Box</code> in two steps: First I introduced a more generic trait <code>CloneIn</code>. Currently this was only for structuring the code better:</p>
<div class="codehilite"><pre><span></span>impl&lt;T: Clone, A: AllocRef, B: BuildAllocRef&gt; CloneIn&lt;A&gt; for Box&lt;T, B&gt;
where
    B::Ref: AllocRef,
{
    type Cloned = Box&lt;T, A::BuildAlloc&gt;;

    fn clone_in(&amp;self, a: A) -&gt; Self::Cloned
    where
        A: AllocRef&lt;Error = crate::Never&gt;,
    {
        Box::new_in(self.as_ref().clone(), a)
    }

    fn try_clone_in(&amp;self, a: A) -&gt; Result&lt;Self::Cloned, A::Error&gt; {
        Box::try_new_in(self.as_ref().clone(), a)
    }
}
</pre></div>


<p>This provides an interface of creating a clone of the current object in an arbitrary allocator. Keep in mind, that <code>Box</code> (and <code>RawVec</code>) holds the ref-builder, not the <code>Ref</code> itself. An <code>AllocRef</code> (or <code>B::Ref</code>) is typically a ZST, a handle, or a reference, which can be cloned cheaply. With this, <code>Clone</code> is pretty straight forward to implement:</p>
<div class="codehilite"><pre><span></span>impl&lt;T: Clone, B: BuildAllocRef + Clone&gt; Clone for Box&lt;T, B&gt;
where
    B::Ref: AllocRef&lt;Error = crate::Never&gt;,
{
    fn clone(&amp;self) -&gt; Self {
        // Clone the builder, this has to be done anyway
        // Now we don&#39;t have to care about `&amp;mut self` anymore
        let mut b = self.build_alloc().clone();
        let old_ptr = self.ptr.cast();
        let old_layout = NonZeroLayout::for_value(self.as_ref());

        unsafe {
            // create a new `B::Ref`
            let a = b.build_alloc_ref(old_ptr, old_layout);
            // clone it
            self.clone_in(a)
        }
    }
}
</pre></div>


<p>It's basically reusing the stored builder.</p>
<p>Does this helps you understanding my thoughts?</p>



<a name="179730838"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/Conversion%20of%20%60bumpalo%60%20to%20use%20%60alloc_wg%60%20crate/near/179730838" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Erich Gubler <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/Conversion.20of.20.60bumpalo.60.20to.20use.20.60alloc_wg.60.20crate.html#179730838">(Nov 03 2019 at 03:54)</a>:</h4>
<p>So, you're saying I should implement this for RawVec first?</p>



<a name="179730946"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/Conversion%20of%20%60bumpalo%60%20to%20use%20%60alloc_wg%60%20crate/near/179730946" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Tim Diekmann <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/Conversion.20of.20.60bumpalo.60.20to.20use.20.60alloc_wg.60.20crate.html#179730946">(Nov 03 2019 at 03:58)</a>:</h4>
<p>I meant <code>Vec</code>, sorry, it's late ;)<br>
On RawVec it doesn't make much sense as it's not aware of the underlying content.</p>



<a name="179742359"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/Conversion%20of%20%60bumpalo%60%20to%20use%20%60alloc_wg%60%20crate/near/179742359" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Gurwinder Singh <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/Conversion.20of.20.60bumpalo.60.20to.20use.20.60alloc_wg.60.20crate.html#179742359">(Nov 03 2019 at 10:46)</a>:</h4>
<p>Hi..<br>
I was also trying to use alloc-wg in bumpalo.<br>
It may be very silly but I am stuck with implementing the <code>BuildAllocRef</code> and <code>AllocRef</code> (and friends) traits. What do you implement them on? directly on <code>Bump</code> or <code>&amp;mut Bump</code>?</p>
<div class="codehilite"><pre><span></span>impl BuildAllocRef for Bump {
    type Ref = &amp;&#39;a mut Bump;  // ??

    unsafe fn build_alloc_ref(
        &amp;mut self,
        ptr: NonNull&lt;u8&gt;,
        layout: Option&lt;NonZeroLayout&gt;,
    ) -&gt; Self::Ref {
        self
    }
}
</pre></div>


<p>We can't clone <code>Bump</code> around.</p>
<p>Also, if I do:</p>
<div class="codehilite"><pre><span></span>impl&lt;&#39;a&gt; BuildAllocRef for &amp;&#39;a mut Bump {
    type Ref = Self;

    unsafe fn build_alloc_ref(
        &amp;mut self,
        ptr: NonNull&lt;u8&gt;,
        layout: Option&lt;NonZeroLayout&gt;,
    ) -&gt; Self::Ref {
        self
    }
}
</pre></div>


<p>That also don't work, because returned reference can't outlive the function's borrowed lifetime.</p>



<a name="179742521"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/Conversion%20of%20%60bumpalo%60%20to%20use%20%60alloc_wg%60%20crate/near/179742521" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Gurwinder Singh <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/Conversion.20of.20.60bumpalo.60.20to.20use.20.60alloc_wg.60.20crate.html#179742521">(Nov 03 2019 at 10:53)</a>:</h4>
<p>I looked around and GAT seems to be the solution. Is it possible to implement this without GATs?</p>



<a name="180044389"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/Conversion%20of%20%60bumpalo%60%20to%20use%20%60alloc_wg%60%20crate/near/180044389" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Erich Gubler <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/Conversion.20of.20.60bumpalo.60.20to.20use.20.60alloc_wg.60.20crate.html#180044389">(Nov 06 2019 at 15:40)</a>:</h4>
<p>They're supposed to be on references/handles to <code>Bump</code>. With the current API, we'll have to use a mutable reference, but I think we need to add another API for synchronized allocator access too.</p>



<a name="180052556"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/Conversion%20of%20%60bumpalo%60%20to%20use%20%60alloc_wg%60%20crate/near/180052556" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Gurwinder Singh <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/Conversion.20of.20.60bumpalo.60.20to.20use.20.60alloc_wg.60.20crate.html#180052556">(Nov 06 2019 at 16:55)</a>:</h4>
<p>Right..  I tried with Bump but cant get it to work</p>



<a name="180052658"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/Conversion%20of%20%60bumpalo%60%20to%20use%20%60alloc_wg%60%20crate/near/180052658" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Gurwinder Singh <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/Conversion.20of.20.60bumpalo.60.20to.20use.20.60alloc_wg.60.20crate.html#180052658">(Nov 06 2019 at 16:55)</a>:</h4>
<p>Global/System ones were simple as they are empty structs that forward calls to global alloc</p>



<a name="180052725"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/Conversion%20of%20%60bumpalo%60%20to%20use%20%60alloc_wg%60%20crate/near/180052725" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Gurwinder Singh <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/Conversion.20of.20.60bumpalo.60.20to.20use.20.60alloc_wg.60.20crate.html#180052725">(Nov 06 2019 at 16:56)</a>:</h4>
<p>For local allocators, borrow checkers gets in thw way</p>



<a name="180052763"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/Conversion%20of%20%60bumpalo%60%20to%20use%20%60alloc_wg%60%20crate/near/180052763" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Tim Diekmann <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/Conversion.20of.20.60bumpalo.60.20to.20use.20.60alloc_wg.60.20crate.html#180052763">(Nov 06 2019 at 16:56)</a>:</h4>
<p><span class="user-mention" data-user-id="219283">@Scott J Maddox</span> If I remembered correctly you had an Idea here when proposing the <code>BuildAlloc</code> approach regarding a bump allocator?</p>



<a name="180053857"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/Conversion%20of%20%60bumpalo%60%20to%20use%20%60alloc_wg%60%20crate/near/180053857" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Erich Gubler <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/Conversion.20of.20.60bumpalo.60.20to.20use.20.60alloc_wg.60.20crate.html#180053857">(Nov 06 2019 at 17:06)</a>:</h4>
<p>I feel like we might want to discuss having a <code>&amp;self</code> variant for allocators that have synchronization. Using <code>&amp;mut self</code> will be nice when we want unsynchronized access to allocator state, but I think many (if not most) allocators will have a synchronization mechanism of some sort to be thread-safe.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>